<h3>Chapter 7: Flow Control</h3>
<p><strong>Review of variables</strong></p>
<p>
Variables may be manipulated by assigning or changing
values with the expressions =, +=, -=, ++,
--. Those expressions may be combined with the expressions -,
+, *, /, %. However, so far, you have only been shown how to use
a function to do these in a linear way. For example:
<pre>
int hello( int x ) {
    x--;
    write(&quot;Hello, x is &quot;+x+&quot;.\n&quot;);
    return x;
}
</pre>
is a function you should know how to write and understand.
But what if you wanted to write the value
of x only if x = 1? Or what if you wanted it to keep writing x
over and over until x = 1 before returning?
LPC uses flow control in exactly the same way as C and C++.
</p>
<hr size="1">
<p><strong>The LPC flow control statements</strong></p>
<p>
LPC uses the following expressions:
<pre>
if( expression )
    instruction;

if( expression )
    instruction;
else
    instruction;

if( expression )
    instruction;
else if( expression )
    instruction;
else
    instruction;

while( expression )
    instruction;

do {
    instruction;
} while( expression );

switch( expression ) {
  case expression:
    instruction;
  break;
  default:
    instruction;
}
</pre>
</p>
<p>
Before we discuss these, first something on what
is meant by expression and instruction. An
expression is anything with a value like a variable, a comparison
(like x&gt;5, where if x is 6 or more, the value is 1, else the
value is 0), or an assignment(like x += 2). An instruction
can be any single line of lpc code like a
function call, a value assignment or modification,
etc.
</p>
<p>
You should know also the operators &amp;&amp;, ||,
==, !=, and !. These are the logical operators.
They return a nonzero value when true, and 0 when false.
</p>
<p>
Make note of the values of the following expressions:
<blockquote>
( 1 &amp;&amp; 1 ) value: 1 ( 1 and 1 )<br>
( 1 &amp;&amp; 0 ) value: 0 ( 1 and 0 )<br>
( 1 || 0 ) value: 1 ( 1 or 0 )<br>
( 1 == 1 ) value: 1 ( 1 is equal to 1 )<br>
( 1 != 1 ) value: 0 ( 1 is not equal to 1 )<br>
( !1 ) value: 0 ( not 1 )<br>
( !0 ) value: 1 ( not 0 )<br>
</blockquote>
In expressions using &amp;&amp;, if the value of
the first item being compared is 0, the second
is never tested even. When using ||, if the first is true
(1), then the second is not tested.
</p>
<hr size="1">
<p><strong>if()</strong></p>
<p>
The first expression to look at that alters flow
control is if(). Take a look at the following
example:
<pre>
1 void reset() {
2     int x;
3
4     ::reset();
5     x = random(10);
6     if( x &gt; 50 ) set_search_func(&quot;floorboards&quot;, &quot;search_floor&quot;);
7 }
</pre>
</p>
<p>
The line numbers are for reference only.
</p>
<p>
In line 2, of course we declare a variable of type int called x.
</p>
<p>
Line 3 is aethetic whitespace
to clearly show where the declarations end and the function
code begins. The variable x is only available to the function
reset().
</p>
<p>
Line 4 makes a call to the room.c version of reset().
</p>
<p>
Line 5 uses the driver efun random() to return a
random number between 0 and the parameter
minus 1. So here we are looking for a number between 0
and 99.
</p>
<p>
In line 6, we test the value of the expression (x&gt;50)
to see if it is true or false. If it is true,
then it makes a call to the room.c function set_search_func().
If it is false, the call to set_search_func() is never
executed.
</p>
<p>
In line 7, the function returns driver control to
the calling function (the driver itself in
this case) without returning any value.
</p>
<p>
If you had wanted to execute multiple instructions
instead of just the one, you would have done
it in the following manner:
<pre>
if( x &gt; 50 ) {
    set_search_func(&quot;floorboards&quot;, &quot;search_floor&quot;);
    if( !present(&quot;beggar&quot;, this_object() ) )
        make_beggar();
}
</pre>
</p>
<p>
Notice the {} encapsulate the instructions to be
executed if the test expression is true. In
the example, again we call the room.c function which sets a function
(search_floor()) that you will later define yourself to
be called when the player types &quot;search floorboards&quot;
(NOTE: This is highly mudlib dependent. Nightmare
mudlibs have this function call.
</p>
<p>
Others may have something similar, while others may
not have this feature under any name). Next,
there is another if() expression that tests the truth
of the expression (!present(&quot;beggar&quot;,this_object())).
The ! in the test expression changes the truth
of the expression which follows it. In this
case, it changes the truth of the efun present(), which will return
the object that is a beggar if it is in the room
(this_object()), or it will return 0 if there
is no beggar in the room. So if there is a beggar still
living in the room, (present(&quot;beggar&quot;, this_object()))
will have a value equal to the beggar object
(data type object), otherwise it will be 0.
The ! will change a 0 to a 1, or any nonzero value (like the
beggar object) to a 0. Therefore, the expression
(!present(&quot;beggar&quot;, this_object())) is
true if there is no beggar in the room, and
false if there is. So, if there is no beggar in the room,
then it calls the function you define in your room
code that makes a new beggar and puts it in
the room. (If there is a beggar in the room, we
do not want to add yet another one :))
</p>
<p>
Of course, if()'s often comes with ands or buts :).
In LPC, the formal reading of the if() statement
is:
<pre>
if( expression ) {
    set of intructions
} else if( expression ) {
    set of instructions
} else {
    set of instructions
}
</pre>
</p>
<p>
This means:
<blockquote>
If expression is true, then do these instructions.
</blockquote>
<blockquote>
Otherise, if this second expression is true, do this
second set.
</blockquote>
<blockquote>
And if none of those were true, then do this last set.
</blockquote>
</p>
<p>
You can have if() alone:
<pre>
if( x &gt; 5 )
    write(&quot;Foo,\n&quot;);
</pre>
with an else if():
<pre>
if( x &gt; 5 )
    write(&quot;X is greater than 5.\n&quot;);
else if( x &gt; 2 )
    write(&quot;X is less than 6, but greater than 2.\n&quot;);
</pre>
with an else:
<pre>
if( x &gt; 5 )
    write(&quot;X is greater than 5.\n&quot;);
else
    write(&quot;X is less than 6.\n&quot;);
</pre>
or the whole lot of them as listed above. You can
have any number of else if()'s in the expression,
but you must have one and only one if() and
at most one else. Of course, as with the beggar example,
you may nest if() statements inside if() instructions.
(For example,
<pre>
if( x &gt; 5 ) {
    if( x == 7 )
        write(&quot;Lucky number!\n&quot;);
    else
        write(&quot;Roll again.\n&quot;);
} else
    write(&quot;You lose.\n&quot;);
</pre>
</p>
<hr size="1">
<p><strong>The statements: while() and do {} while()</strong></p>
<p>
Prototype:
<pre>
while( expression ) {
    set of instructions
}

do {
   set of instructions
} while( expression );
</pre>
These allow you to create a set of instructions which
continue to execute so long as some expression
is true. Suppose you wanted to set a variable
equal to a player's level and keep subtracting random amounts
of either money or hp from a player until that variable equals
0 (so that player's of higher levels would lose more).
You might do it this way:
<pre>
1 int x;
2
3 x = (int)this_player()-&gt;query_level(); /* this has yet to be explained */
4 while( x &gt; 0 ) {
5     if( random(2) ) this_player()-&gt;add_money(&quot;silver&quot;, -random(50) );
6     else this_player()-&gt;add_hp(-(random(10) );
7     x--;
8 }
</pre>
The expression this_player()-&gt;query_level() call. In line 4,
we start a loop that executes so long as x is greater than 0.
</p>
<p>
Another way we could have done this line would be:
<pre>
while(x) {
</pre>
The problem with that would be if we later made a
change to the funtion y anywhere between 0
and 49 coins.
</p>
<p>
In line 6, if instead it returns 0, we call the add_hp()
function in the player which reduces the player's
hit points anywhere between 0 and 9 hp.
</p>
<p>
In line 7, we reduce x by 1.
</p>
<p>
At line 8, the execution comes to the end of the
while() instructions and goes back up to line
4 to see if x is still greater than 0. This loop
will keep executing until x is finally less than 1.
</p>
<p>
You might, however, want to test an expression <b>after</b>
you execute some instructions. For instance,
in the above, if you wanted to execute the
instructions at least once for everyone, even if their level is
below the test level:
<pre>
int x;

x = (int)this_player()-&gt;query_level();

do {
    if( random(2) )
        this_player()-&gt;add_money(&quot;silver&quot;, -random(50) );
    else
        this_player()-&gt;add_hp(-random(10) );
    x--;
} while( x &gt; 0 );
</pre>
This is a rather bizarre example, being as few muds
have level 0 players. And even still, you
could have done it using the original loop with a
different test. Nevertheless, it is intended to show how a do{}
while() works. As you see, instead of initiating
the test at the beginning of the loop (which
would immediately exclude some values of x), it tests after
the loop has been executed. This assures that the
instructions of the loop get executed at least
one time, no matter what x is.
</p>
<hr size="1">
<p><strong>for() loops</strong></p>
<p>
Prototype:
<pre>
for( initialize values ; test expression ; instruction ) {
    instructions
}
</pre>
</p>
<p>
initialize values:
<blockquote>
This allows you to set starting values of variables
which will be used in the loop. This part
is optional.
</blockquote>
test expression:
<blockquote>
Same as the expression in if() and while(). The loop
is executed as long as this expression (or
expressions) is true. You must have a test
expression.
</blockquote>
instruction:
<blockquote>
An expression (or expressions) which is to be executed
at the end of each loop. This is optional.
</blockquote>
Note:
<pre>
for( ; expression; ) {}
</pre>
IS EXACTLY THE SAME AS
<pre>
while( expression ) {}
</pre>
</p>
<p>
Example:
<pre>
1 int x;
2
3 for( x = (int)this_player()-&gt;query_level(); x&gt;0; x-- ) {
4     if( random(2) ) this_player()-&gt;add_money(&quot;silver&quot;, -random(50) );
5     else this_player()-&gt;add_hp(-random(10) );
6 }
</pre>
This for() loop behaves EXACTLY like the while()
example. Additionally, if you wanted to initialize 2 variables:
<pre>
for( x = 0, y = random(20); x&lt;y; x++ ) {
    write(x+&quot;\n&quot;);
}
</pre>
Here, we initialize 2 variables, x and y, and we
separate them by a comma. You can do the same
with any of the 3 parts of the for() expression.
</p>
<hr size="1">
<p><strong>The statement: switch()</strong></p>
<p>
Prototype:
<pre>
switch( expression ) {
  case constant:
    instructions
  case constant:
    instructions
  ...
  case constant:
    instructions
  default:
    instructions
}
</pre>
This is functionally much like if() expressions,
and much nicer to the CPU, however most rarely
used because it looks so damn complicated.
</p>
<p>
But it is not.
</p>
<p>
First off, the expression is not a test. The cases
are tests. A English sounding way to read:
<pre>
1 int x;
2
3 x = random(5);
4 switch( x ) {
5   case 1: write(&quot;X is 1.\n&quot;);
6   case 2: x++;
7   default: x--;
8 }
9 write( x + &quot;\n&quot;);
</pre>
is:
</p>
<p>
set variable x to a random number between 0 and 4.
In case 1 of variable x write its value add 1 to
it and subtract 1. In case 2 of variable x,
add 1 to its value and then subtract 1. In
other cases subtract 1. Write the value of
x. switch(x) basically tells the driver that
the variable x is the value we are trying
to match to a case.
</p>
<p>
Once the driver finds a case which matches, that
case <b>and all following cases</b>
will be acted upon. You may break out of the switch statement
as well as any other flow control statement with a break instruction
in order only to execute a single case. But
that will be explained later. The default
statement is one that will be executed for any value of
x so long as the switch() flow has not been broken.
You may use any data type in a switch statement:
<pre>
string name;

name = (string)this_player()-&gt;query_name();

switch( name ) {
  case &quot;descartes&quot;:
      write(&quot;You borg.\n&quot;);
  case &quot;flamme&quot;:
  case &quot;forlock&quot;:
  case &quot;shadowwolf&quot;:
    write(&quot;You are a Nightmare head arch.\n&quot;);
  default:
    write(&quot;You exist.\n&quot;);
}
</pre>
For me, I would see:
<pre>
You borg.
You are a Nightmare head arch.
You exist.
</pre>
Flamme, Forlock, or Shadowwolf would see:
<pre>
You are a Nightmare head arch.
You exist.
</pre>
Everyone else would see:
<pre>
You exist.
</pre>
</p>
<hr size="1">
<p><strong>Altering the flow of functions and flow control statements</strong></p>
<p>
The following instructions:
<pre>
return continue break
</pre>
alter the natural flow of things as described above.
</p>
<p>return
<blockquote>
No matter where it occurs in a function, will cease
the execution of that function and return control to the function
which called the one the return statement
is in. If the function is NOT of type void, then a value
must follow the return statement, and that value must be of a
type matching the function. An absolute value function
would look like this:
<pre>
int absolute_value( int x ) {
  if( x &gt; -1 )
      return x;
  else
      return -x;
}
</pre>
In the second line, the function ceases execution
and returns to the calling function because
the desired value has been found if x is a positive number.
</blockquote>
</p>
<p>continue
<blockquote>
This is most often used in for() and while statements.
It serves to stop the execution of the current
loop and send the execution back to the beginning
of the loop. For instance, say you wanted to avoid division
by 0:
<pre>
x = 4;

while( x &gt; -5 ) {
    x--;
    if( !x )
        continue;
    write( ( 100 / x )+&quot;\n&quot;);
}

write(&quot;Done.\n&quot;)
</pre>
You would see the following output:
<pre>
33
50
100
-100
-50
-33
-25
Done.
</pre>
To avoid an error, it checks in each loop to make
sure x is not 0. If x is zero, then it starts
back with the test expression without finishing
its current loop.
</blockquote>
<blockquote>
In a for() expression
<pre>
for( x = 3; x &gt; -5; x-- ) {
    if( !x )
        continue;
    write( ( 100 / x )+&quot;\n&quot;);
}
write(&quot;Done.\n&quot;);
</pre>
It works much the same way. Note this gives exactly
the same output as before. At x=1, it tests
to see if x is zero, it is not, so it writes
100/x, then goes back to the top, subtracts one from x, checks
to see if it is zero again, and it is zero,
so it goes back to the top and subtracts 1
again.
</blockquote>
</p>
<p>
break
<blockquote>
This one ceases the function of a flow control statement.
No matter where you are in the statement, the control of the
program will go to the end of the loop. So, if in the above examples,
we had used break instead of continue, the output would
have looked like this:
<pre>
33
50
100
Done.
</pre>
</blockquote>
</p>

<p>
continue is most often used with the for() and while()
statements. break however is mostly used with switch()
<pre>
switch( name ) {
  case &quot;descartes&quot;:
    write(&quot;You are borg.\n&quot;);
  break;
  case &quot;flamme&quot;:
    write(&quot;You are flamme.\n&quot;);
  break;
  case &quot;forlock&quot;:
    write(&quot;You are forlock.\n&quot;);
  break;
  case &quot;shadowwolf&quot;:
    write(&quot;You are shadowwolf.\n&quot;);
  break;
  default:
    write(&quot;You will be assimilated.\n&quot;);
}
</pre>
This functions just like:
<pre>
if( name == &quot;descartes&quot; )
    write(&quot;You are borg.\n&quot;);
else if( name == &quot;flamme&quot;)
    write(&quot;You are flamme.\n&quot;);
else if( name == &quot;forlock&quot;)
    write(&quot;You are forlock.\n&quot;);
else if( name == &quot;shadowwolf&quot;)
    write(&quot;You are shadowwolf.\n&quot;);
else
    write(&quot;You will be assimilated.\n&quot;);
</pre>
except the switch statement is much better on the CPU.
</p>
<p>
If any of these are placed in nested statements,
then they alter the flow of the most immediate
statement.
</p>
<hr size="1">
<p><strong>Chapter Summary</strong></p>
<p>
This chapter covered one hell of a lot, but it was
stuff that needed to be seen all at once.
You should now completely understand if() for() while()
do{} while() and switch(), as well as how to alter their flow
using return, continue, and break. Efficiency says
if it can be done in a natural way using switch()
instead of a lot of if() else if()'s, then by
all means do it. You were also introduced to the idea of calling
functions in other objects. That however, is a topic
to be detailed later.
</p>
<p>
You now should be completely at ease writing simple rooms (if you have read
your mudlib's room building document), simple monsters, and other sorts of simple
objects.
</p>
